package com.shuigu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.shuigu.utils.SecurityUtils;
import com.shuigu.pojo.dto.UmsUserParamDto;
import com.shuigu.pojo.dto.UserRoleDto;
import com.shuigu.pojo.entity.UmsSysUser;
import com.shuigu.mapper.UmsSysUserMapper;
import com.shuigu.response.PageResult;
import com.shuigu.service.ISysUserService;
import com.shuigu.service.IUmsSysUserService;
import com.shuigu.utils.OffMp;
import com.shuigu.utils.ToMp;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UmsSysUserServiceImpl extends ServiceImpl<UmsSysUserMapper, UmsSysUser> implements IUmsSysUserService {

    private final PasswordEncoder passwordEncoder;
    private final ISysUserService sysUserService;

    public UmsSysUserServiceImpl(PasswordEncoder passwordEncoder, ISysUserService sysUserService) {
        this.passwordEncoder = passwordEncoder;
        this.sysUserService = sysUserService;
    }

    @Override
    public PageResult<UmsSysUser> listByPage(UmsUserParamDto userParamDto) {

        Page<UmsSysUser> mpPage = new ToMp().toMpPage(userParamDto);
        Page<UmsSysUser> page = lambdaQuery().eq(userParamDto.getUsername() != null && !userParamDto.getUsername().isEmpty(), UmsSysUser::getUsername, userParamDto.getUsername())
                .like(userParamDto.getNickname() != null, UmsSysUser::getNickname, userParamDto.getNickname())
                .eq(userParamDto.getEmail() != null && !userParamDto.getEmail().isEmpty(), UmsSysUser::getEmail, userParamDto.getEmail())
                .eq(userParamDto.getMobile() != null && !userParamDto.getMobile().isEmpty(), UmsSysUser::getMobile, userParamDto.getMobile())
                .eq(userParamDto.getSex() != null, UmsSysUser::getSex, userParamDto.getSex())
                .eq(userParamDto.getStatus() != null, UmsSysUser::getStatus, userParamDto.getStatus())
                .page(mpPage);
        return OffMp.of(page);
    }

    @Transactional
    @Override
    public boolean saveSysUser(UmsSysUser user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        String username = SecurityUtils.getUsername();
        user.setCreateUser(username);
        user.setUpdateUser(username);

        boolean save = save(user);
        // 为创建用户初始化普通用户角色
        UserRoleDto userRoleDto = UserRoleDto.builder()
                .userId(user.getId())
                .roleId(Long.valueOf(Integer.valueOf(3).toString()))
                .build();
        sysUserService.insertUserRole(userRoleDto);
        return save;
    }

    @Override
    public boolean updateSysUser(UmsSysUser user) {
        // 对密码等字段进行非空与空值校验

        // 判断是否修改密码
        UmsSysUser byId = getById(user.getId());
        if(user.getPassword() != byId.getPassword()){
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        }
        user.setUpdateUser(SecurityUtils.getUsername());
        return updateById(user);
    }
}
